#ifndef VNCRECVTHREAD_H
#define VNCRECVTHREAD_H

#include <QThread>

#include <QImage>

#include "rfb/rfbclient.h"

class VNCRecvThread : public QThread
{
    Q_OBJECT
public:
    VNCRecvThread(QObject* parent = nullptr);

    inline void startRun(rfbClient* cl){
        if(_runFlag)return;
        _cl = cl;
        _cl->FinishedFrameBufferUpdate = frameBufferUpdated;
        rfbClientSetClientData(_cl, nullptr, this);
        _runFlag = true;
        this->start();
    }

    inline void stopRun(){
        if(!_runFlag)return;
        _runFlag = false;
        if(_cl)rfbClientSetClientData(_cl, nullptr, nullptr);
        if(_cl)_cl->FinishedFrameBufferUpdate = nullptr;
        _cl = nullptr;
    }

    static void frameBufferUpdated(rfbClient* cl);

protected:
    void run() override;

private:
    bool _runFlag = false;
    rfbClient* _cl;

signals:
    void updateImageSignal(QImage);
};

#endif // VNCRECVTHREAD_H
